using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.IO;
using Xeml.Document.Contracts;
using Xeml.Document;

namespace XemlInteractiveDesigner
{
    public partial class SourcecodeForm : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private string _sourceCode;
        private IDocument _xemlDoc = new XemlDocument();

        public string SourceCode
        {
            get { return _sourceCode; }
            set { _sourceCode = value;
            this.syntaxDocumentXeml.Text = _sourceCode;
            _xemlDoc.DoXmlValidation(syntaxDocumentXeml.Text);
            }
        }

        public SourcecodeForm()
        {
            InitializeComponent();
            SetupSyntaxDoc();
            _xemlDoc.Validation += new EventHandler<ValidationMessage>(OnXemlDocValidation);
           // _xemlDoc.DoXmlValidation("test");
        }

       
        void OnXemlDocValidation(object sender, ValidationMessage e)
        {

            XmlValidationMessage xe = e as XmlValidationMessage;
            IValidator iv = sender as IValidator;
            //this.validationLog.Log.NewLogRow
            ValidationLog.LogRow lr = validationLog.Log.NewLogRow();
            switch (e.SeverityCode)
            {
                case SeverityCode.Error:
                    {
                        //this.hasErrors = true;
                        lr.SeverityCode = "Error";
                        break;
                    }
                case SeverityCode.Message:
                    {
                        lr.SeverityCode = "Info";
                        break;
                    }
                case SeverityCode.Warning:
                    {
                        lr.SeverityCode = "Warning";
                        break;
                    }
            }

            lr.Message = e.Text;
            if (e.RelatedObject != null)
                lr.RelatedObject = e.RelatedObject.ToString();
            //string[] className = sender.ToString().Split('.');
          //  if (e.RelatedObject != null)


             lr.Source = sender.GetType().Name;// className[className.Length - 1];



            if (e.InhibitRendering)
            {
              //  this.invalidDocument = true;
                lr.DocumentError = true;
            }
            else
            {
                lr.DocumentError = false;
            }

            lr.Line = xe.Line + 1;
            lr.Column = xe.Column + 1;

            validationLog.Log.Rows.Add(lr);
        }
    

        private void SetupSyntaxDoc()
        {
            string tempPath = Path.GetTempPath();
            tempPath += "\\xml.syn";

            File.WriteAllBytes(tempPath, MainResources.XMLSyntax);
            syntaxDocumentXeml.SyntaxFile = tempPath;


        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            codeEditorControl.Cut();
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            codeEditorControl.Copy();
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            codeEditorControl.Paste();
        }

        private void kryptonButton2_Click(object sender, EventArgs e)
        {
            _sourceCode = syntaxDocumentXeml.Text;
            this.Close();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void syntaxDocumentXeml_Change(object sender, EventArgs e)
        {
            validationLog.Log.Rows.Clear();
            _xemlDoc.DoXmlValidation(syntaxDocumentXeml.Text);
        }
    }
}